package com.woniuxy.intellihome.service.impl;

import com.woniuxy.intellihome.entity.Orderitem;
import com.woniuxy.intellihome.entity.Sku;
import com.woniuxy.intellihome.entity.VGoods;
import com.woniuxy.intellihome.mapper.OrderitemMapper;
import com.woniuxy.intellihome.mapper.VgoodsMapper;
import com.woniuxy.intellihome.service.OrderitemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.intellihome.service.SkuService;
import com.woniuxy.intellihome.service.VgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pengZhihong
 * @since 2021-08-12
 * 1、添加了 getByOrderId 方法用于获取订单明细详细情况
 * 2、goods采用String数组的方式记录购买型号与信息
 */
@Service
public class OrderitemServiceImpl extends ServiceImpl<OrderitemMapper, Orderitem> implements OrderitemService {
    @Autowired
    private OrderitemMapper orderitemMapper;
    @Autowired
    private VgoodsMapper vgoodsMapper;
    @Autowired
    private SkuService skuService;

    //获取Goods信息存入，并返回对应订单详情
    @Override
    public List<Orderitem> getByOrderId(int orderId) {
        List<Orderitem> orderitems = orderitemMapper.getByOrderId(orderId);
        //将商品信息存入对应orderitem
        for (Orderitem orderitem : orderitems) {
            List<String> goods = vgoodsMapper.getAllSpecValues(orderitem.getSkuId());
            //获取第一个vgoods对象
            VGoods vgoods = vgoodsMapper.getDetailById(orderitem.getSkuId()).get(0);
            orderitem.setGoodsName(vgoods.getName());
            orderitem.setGoodsSpec(goods);
        }
        return orderitems;
    }


    public List<Orderitem> getWithSKUByOrderId(int orderId){
        List<Orderitem> orderitems = orderitemMapper.getByOrderId(orderId);
        for(Orderitem orderitem : orderitems){
            Sku sku = skuService.getWithSpuSpecDetailBySKUId(orderitem.getSkuId());

            orderitem.setSku(sku);
        }
        return orderitems;
    }
}
